﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using VonaJobs.DAL.DAO;

namespace VonaJobs.DAL.Gatway
{
    public class MemberTypeGateway:DBGateway
    {
        public string SaveNewMemberType(MemberType memberType)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText =
                    string.Format("insert into tblMemberType values(@TypeName)");
                CommandObj.Parameters.Clear();
                CommandObj.Parameters.Add("@TypeName", SqlDbType.NVarChar).Value = memberType.TypeName;
                //CommandObj.Parameters.Add("@PolicyId", SqlDbType.Int).Value = memberType.Policy.Id;
                CommandObj.ExecuteNonQuery();
                return "New member type created successfully";
            }
            catch (Exception)
            {
                throw new Exception("Message Not Send, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
        }

        public bool HasThisMemebrShipExist(MemberType memberType)
        {
            try
            {
                ConnectionObj.Open();
                CommandObj.CommandText = String.Format("Select * From tblMemberType Where Id='{0}'", memberType.Id);
                SqlDataReader reader = CommandObj.ExecuteReader();
                while (reader.Read())
                {
                    return true;
                }


            }
            catch (Exception)
            {
                throw new Exception("Membership checking failed, Server Error !");
            }
            finally
            {
                if (ConnectionObj != null && ConnectionObj.State == ConnectionState.Open)
                {
                    ConnectionObj.Close();
                }

            }
            return false;
        }

        public string UpdateMemberType(MemberType memberType)
        {
            try
            {
                ConnectionObj.Open();
                string update = String.Format("Update tblMemberType Set TypeName='{0}' where Id='{1}'", memberType.TypeName, memberType.Id);
                CommandObj.CommandText = update;
                CommandObj.ExecuteNonQuery();

            }
            catch (Exception)
            {
                
                throw;
            }
            return "MemberShiptype update successfully";
        }

        public List<MemberType> GetAllMemberType()
        {
            try
            {
                List<MemberType> MemberList = new List<MemberType>();
                ConnectionObj.Open();
                CommandObj.CommandText = "Select * From tblMemberType";
                SqlDataReader reader = CommandObj.ExecuteReader();

                while (reader.Read())
                {
                    MemberType memberType = new MemberType();
                    memberType.Id = Convert.ToInt32(reader[0]);
                    memberType.TypeName = reader[1].ToString();
                    MemberList.Add(memberType);
                }

                return MemberList;
               
            }
            catch (Exception)
            {
                
                throw;
            }
            finally
            {
                ConnectionObj.Close();
            }
        }
    }
}